import { createStore, applyMiddleware, compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import rootReducer from './reducers/index';

export default function configureStore(preloadedState) {
    const middlewares = [thunkMiddleware];
    if (process.env.NODE_ENV === 'development') {
        const {logger} = require('redux-logger');
        middlewares.push(logger);
    }
    const store = compose(applyMiddleware(...middlewares))(createStore)(rootReducer);

    if (module.hot) {
        // Enable Webpack hot module replacement for reducers
        module.hot.accept('./reducers', () => {
            const nextRootReducer = require('./reducers/index');
            store.replaceReducer(nextRootReducer);
        });
    }
    return store;
};
